#include "pbmsg.hpp"
#include <cstdio>
#include "utils.h"
using namespace std;

int main(int argc, char* argv[])
{
    int tid = 112;
    std::string name = "buxk";
    int syd = 100;
    // 根据Proto文件流创建。
    {
        std::string proto_str = R"(            
syntax = "proto3";
message Person {
  string name = 1;
  int32 id = 2;  // Unique ID number for this person.
  string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    string number = 1;
    PhoneType type = 2;
  }

  repeated PhoneNumber phones = 4;
}

// Our address book file is just one of these.
message AddressBook {

  message req_data {
    int32 tid = 1;
    uint64 seq = 2;
    string name = 3;
  }
  
  int32 syd = 1;
  repeated Person people = 2;
  req_data data = 3;
}
)";
        printf("通过反射创建对象 >>> \n");

        std::shared_ptr<pbmsg_t>  reqdata = pbmsg_t::create("addressbook.proto", proto_str, "AddressBook.req_data");

        reqdata->set_attr("tid", tid);
        printf(" set data.tid = %d\n", tid);
        reqdata->set_attr("name", name);
        printf("  set data.name = %s\n", name.c_str());

        std::shared_ptr<pbmsg_t>  addressbook = pbmsg_t::create("addressbook.proto", proto_str, "AddressBook");
        addressbook->set_attr("syd", syd);
        printf("  set syd = %d\n", syd);

        addressbook->set_attr("data", reqdata->get_msg());
        std::string result = addressbook->get_bin();

        printf("-----------------\n  result length = %zu\n", result.length());
        auto s = dump_mem_to_str(result.data(), result.length(), 20, 7);
        printf("%s", s.c_str());
        printf("-----------------\n");

        std::shared_ptr<pbmsg_t> clone_req_data = reqdata->clone();

        int clone_tid = 112;
        reqdata->get_attr("tid", clone_tid);
        printf(" get clone data.tid = %d\n", clone_tid);
        std::string clone_name;
        reqdata->get_attr("name", clone_name);
        printf(" get clone data.name = %s\n", clone_name.c_str());

      std::string json_str = reqdata->get_json();
        printf(" get json = %s\n", json_str.c_str());

        reqdata->set_json(R"({"tid":110,"name":"kermit"})");
      
        result = reqdata->get_bin();
        printf("-----------------\n  result length = %zu\n", result.length());
        s = dump_mem_to_str(result.data(), result.length(), 20, 7);
        printf("%s", s.c_str());
        printf("-----------------\n");
        
    }

    return 0;
}